Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | export const dynamic = "force-dynamic"; /** * Dev Sprint Detail API * GET /api/dev/sprints/[id] - Get a single sprint with tickets and stats * PATCH /api/dev/sprints/[id] - Update a sprint * DELETE /api/dev/sprints/[id] - Delete a sprint */ import { NextRequest, NextResponse } from 'next/server'; import { Session } from "next-auth"; import { withAdmin, withErrorHandling, successResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { RouteContext } from "@/lib/api/middleware"; import type { AuthenticatedUser } from '@/lib/api/middleware/types'; import { prisma } from '@/lib/prisma'; import { UpdateDevSprintSchema } from '@/lib/validation/dev-ticket-schemas'; import { getSprintProgress } from '@/lib/dev-ticket'; import { logger } from '@/lib/logging'; interface RouteParams { params: Promise<{ id: string }>; } async function handleGet( request: NextRequest, context: RouteContext | undefined ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const sprint = await prisma.devSprint.findUnique({ where: { id }, include: { project: { select: { id: true, name: true, key: true, color: true } }, tickets: { include: { assignee: { select: { id: true, name: true, email: true, image: true } }, labels: true }, orderBy: [{ status: 'asc' }, { priority: 'desc' }] }, _count: { select: { tickets: true } } } }); if (!sprint) { throw ApiError.notFound('Sprint not found'); } // Get detailed progress stats const progress = await getSprintProgress(id); // Get ticket breakdown by status const ticketsByStatus = await prisma.devTicket.groupBy({ by: ['status'], where: { sprintId: id }, _count: true }); // Get ticket breakdown by assignee const ticketsByAssignee = await prisma.devTicket.groupBy({ by: ['assigneeId'], where: { sprintId: id }, _count: true, _sum: { storyPoints: true } }); // Fetch assignee details const assigneeIds = ticketsByAssignee .map((t) => t.assigneeId) .filter((id): id is number => id !== null); const assignees = await prisma.user.findMany({ where: { id: { in: assigneeIds } }, select: { id: true, name: true, email: true, image: true } }); const assigneeMap = new Map(assignees.map((a) => [a.id, a])); const workloadByAssignee = ticketsByAssignee.map((item) => ({ assignee: item.assigneeId ? assigneeMap.get(item.assigneeId) : null, ticketCount: item._count, storyPoints: item._sum.storyPoints || 0 })); return successResponse({ ...sprint, stats: { ...progress, byStatus: Object.fromEntries(ticketsByStatus.map((s) => [s.status, s._count])), byAssignee: workloadByAssignee } }); } async function handlePatch( request: NextRequest, context: RouteContext | undefined, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const body = await request.json(); const validationResult = UpdateDevSprintSchema.safeParse(body); if (!validationResult.success) { throw ApiError.validation("Invalid sprint data", validationResult.error.flatten().fieldErrors); } // Check if sprint exists const existingSprint = await prisma.devSprint.findUnique({ where: { id } }); if (!existingSprint) { throw ApiError.notFound('Sprint not found'); } const data = validationResult.data; // If activating sprint, check no other active sprint in project if (data.status === 'ACTIVE' && existingSprint.status !== 'ACTIVE') { const activeSprint = await prisma.devSprint.findFirst({ where: { projectId: existingSprint.projectId, status: 'ACTIVE', id: { not: id } } }); if (activeSprint) { throw ApiError.badRequest( `Cannot activate sprint. "${activeSprint.name}" is already active.` ); } } // Update sprint const sprint = await prisma.devSprint.update({ where: { id }, data, include: { project: { select: { id: true, name: true, key: true, color: true } }, _count: { select: { tickets: true } } } }); logger.info(`Updated sprint "${sprint.name}"`, { category: 'DEV_SPRINTS', sprintId: id, userId: user.id, changes: Object.keys(data) }); return successResponse(sprint); } async function handleDelete( request: NextRequest, context: RouteContext | undefined, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; // Check if sprint exists const sprint = await prisma.devSprint.findUnique({ where: { id }, include: { _count: { select: { tickets: true } } } }); if (!sprint) { throw ApiError.notFound('Sprint not found'); } // Prevent deletion of active sprints if (sprint.status === 'ACTIVE') { throw ApiError.badRequest('Cannot delete an active sprint. Complete or cancel it first.'); } // Remove sprint reference from tickets (don't delete tickets) if (sprint._count.tickets > 0) { await prisma.devTicket.updateMany({ where: { sprintId: id }, data: { sprintId: null } }); } // Delete sprint await prisma.devSprint.delete({ where: { id } }); logger.info(`Deleted sprint "${sprint.name}"`, { category: 'DEV_SPRINTS', sprintId: id, userId: user.id, ticketsUnlinked: sprint._count.tickets }); return successResponse({ message: 'Sprint deleted successfully', ticketsUnlinked: sprint._count.tickets }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); export const DELETE = withErrorHandling(withAdmin(handleDelete)); |